<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_model extends CI_Model{
	
	var $image_path = "";
	var $image_path_url = "";
	
	function __construct(){
		//spara på disken path
		$this->image_path = realpath(APPPATH . '../stories');
		//path till bilden 
		$this->image_path_url = base_url().'/stories';
	}
	
	function do_upload($title, $story, $category, $caption, $path_title){
	
		//ta ut bildens egenskaper
		list($width, $height, $type, $attr) = getimagesize($_FILES['userfile']['tmp_name']);

		if($width != 990 || $height != 600){
			echo "Bilden måste vara 990x600";
		}
		else{
			//annars ladda upp bild
			mkdir($this->image_path. '/\/' .$path_title, 0777, true);
			mkdir($this->image_path. '/\/' . $path_title . '\thumbs', 0777, true);

			$config = array(
				'allowed_types' => 'jpg|jpeg|png',
				'upload_path' => $this->image_path.'/\/'.$path_title,
				'max_size' => 5000,
				'max_width' => 990,
				'max_height' => 600
			);
			
			$this->load->library('upload', $config);
			$this->upload->do_upload();

			//all data om bilden finns att hämta i denna variabel
			$image_data = $this->upload->data();
			//var_dump($image_data);
		
			//Config array för att skapa thumbnails
			$config = array(
				'source_image' => $image_data['full_path'],
				'new_image' => $this->image_path.'/\/'.$path_title.'/\/thumbs',
				'maintain_ration' => true,
				'width' => 150,
				'height' => 100,
				'encrypt_name' => true
			);
		
			$this->load->library('image_lib', $config);
			$this->image_lib->resize();

			//spara allt i databasen m.h.a active records
			$data = array(
				'id' => $title,
			   	'title' => $title ,
			   	'description' => $story ,
			   	'caption' => $caption,
			   	'category' => $category,
			   	'dateadded' => date("Y-m-d")
	 		);
			$this->db->insert('posts', $data); 

			//spara i image tabellen
			$data = array(
			   	'storyid' => $title ,
				'imageurl' => $this->image_path_url.'/'.$path_title.'/'.$image_data['file_name'],
	 		);
			$this->db->insert('images', $data); 

			//spara i thumbs tabellen
			$data = array(
			   	'storyid' => $title ,
				'thumburl' => $this->image_path_url.'/'.$path_title.'/thumbs/'.$image_data['file_name']
	 		);
			$this->db->insert('thumbs', $data); 
		}
	}
}

/*
'file_name' => string 'bp101.jpg' (length=9)
  'file_type' => string 'image/jpeg' (length=10)
  'file_path' => string 'C:/Users/Jarmo/Desktop/php2projekt/stories/City/' (length=48)
  'full_path' => string 'C:/Users/Jarmo/Desktop/php2projekt/stories/City/bp101.jpg' (length=57)
  'raw_name' => string 'bp101' (length=5)
  'orig_name' => string 'bp10.jpg' (length=8)
  'client_name' => string 'bp10.jpg' (length=8)
  'file_ext' => string '.jpg' (length=4)
  'file_size' => float 741.7
  'is_image' => boolean true
  'image_width' => int 990
  'image_height' => int 600
  'image_type' => string 'jpeg' (length=4)
  'image_size_str' => string 'width="990" height="600"' (length=24)
  */


 
		//

		//var_dump($width . $height);
		//skapar underkatalog i stories, katalognamn är lika med story titel
	/*	mkdir($this->image_path. '/\/' .$path_title, 0777, true);
		mkdir($this->image_path. '/\/' . $path_title . '\thumbs', 0777, true);

		$config = array(
			'allowed_types' => 'jpg|jpeg|png',
			'upload_path' => $this->image_path.'/\/'.$path_title,
			'max_size' => 5000,
			'max_width' => 990,
			'max_height' => 600
		);

		$this->load->library('upload', $config);
		$this->upload->do_upload();

		//all data om bilden finns att hämta i denna variabel
		$image_data = $this->upload->data();
		//var_dump($image_data);
	
		//Config array för att skapa thumbnails
		$config = array(
			'source_image' => $image_data['full_path'],
			'new_image' => $this->image_path.'/\/'.$path_title.'/\/thumbs',
			'maintain_ration' => true,
			'width' => 150,
			'height' => 100,
			'encrypt_name' => true
		);
		
		$this->load->library('image_lib', $config);
		$this->image_lib->resize();

		//spara allt i databasen m.h.a active records
		$data = array(
			'id' => $title,
		   	'title' => $title ,
		   	'description' => $story ,
		   	'caption' => $caption,
		   	'category' => $category,
		   	'dateadded' => date("Y-m-d")
 		);
		$this->db->insert('posts', $data); 

		//spara i image tabellen
		$data = array(
		   	'storyid' => $title ,
			'imageurl' => $this->image_path_url.'/'.$path_title.'/'.$image_data['file_name'],
 		);
		$this->db->insert('images', $data); 

		//spara i thumbs tabellen
		$data = array(
		   	'storyid' => $title ,
			'thumburl' => $this->image_path_url.'/'.$path_title.'/thumbs/'.$image_data['file_name']
 		);
		$this->db->insert('thumbs', $data); 
		*/